package cn.edu.scut.sys.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.UnitService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.Leader;
import cn.edu.scut.sys.vo.Unit;

@Controller
@RequestMapping("/unit")
public class UnitrController {

	private UnitService unitService;

	@Resource(name="unitService")
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<Unit> list(
			@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters,
            HttpServletRequest request
            ){  
		 try {  
	            DataRequest dataRequest = new DataRequest();  
	            dataRequest.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            dataRequest.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            dataRequest.setSidx(sidx);  
	            dataRequest.setSord(sord);  
	            dataRequest.setSearch(search);  
	            dataRequest.setSearchField(searchField);  
	            dataRequest.setSearchOper(searchOper);  
	            dataRequest.setSearchString(searchString);
	            return unitService.list(dataRequest, request.getParameter("unitId"));  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String name, String type, String unitId, HttpServletRequest request){
		try{
			Unit unit = new Unit();
			unit.setName(name);
			unit.setType(Integer.parseInt(type));
			Unit u = new Unit();
			u.setId(Long.parseLong(unitId));
			unit.setUnit(u);
			unitService.add(unit);
			request.setAttribute("unit", unit);
			request.setAttribute("status", 1);
			return "/page/unitAdd";
		}catch(Exception ex){
			request.setAttribute("status", 2);
			return "/page/unitAdd";
		}
		
	}
	
	@RequestMapping("/toList/{id}")
	public String toList(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("unit", unitService.get(id));
		return "/page/unitList";
	}
	
	@RequestMapping("/toAdd/{id}")
	public String toAdd(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("unit", unitService.get(id));
		return "/page/unitAdd";
	}
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("unit", unitService.get(id));
		return "/page/unitEdit";
	}
	
	@RequestMapping("/update")
	public String update(String id, String name, HttpServletRequest request){
		try{
			Unit unit = unitService.get(Long.parseLong(id));
			unit.setName(name);
			unitService.update(unit);
			request.setAttribute("unitId", unit.getUnit().getId());
			request.setAttribute("status", 1);
			return "/page/unitEdit";
		}catch(Exception ex){
			request.setAttribute("status", 0);
			return "/page/unitEdit";
		}
	}
	
	@RequestMapping("/setLeaderPage/{id}")
	public String setLeaderPage(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("unit", unitService.get(id));
		return "/page/unitLeaderList";
	}
	
	@RequestMapping("/setLeader")
	public String setLeader(Long leaderId, Long unitId, HttpServletRequest request){
		Unit unit = unitService.get(unitId);
		Leader leader = new Leader();
		leader.setId(leaderId);
		unit.setLeader(leader);
		unitService.update(unit);
		return "/page/unitList";
	}
	
	@RequestMapping("/setLeaderNull")
	public String setLeaderNull(Long unitId, HttpServletRequest request){
		Unit unit = unitService.get(unitId);
		unit.setLeader(null);
		unitService.update(unit);
		return "/page/unitList";
	}
	
	@RequestMapping("/getUnitList/{unitId}")
	@ResponseBody
	public List<Unit> getUnitList(@PathVariable(value = "unitId") Long unitId){
		return unitService.listByUnitId(unitId);
	}
}
